#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：zm_pom 
@File    ：login_page.py
@IDE     ：PyCharm 
@Author  ：你的烂笔头
@Date    ：2025/6/6 22:40 
'''
import logging
import os
import time

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException

from common.base_page import BasePage
from utils.json_handler import JsonHandler
from configs.path_conf import *


from common.logcof import log
from configs.url_config import urls

class LoginPage(BasePage):

    def __init__(self, driver: webdriver.Remote):
        super().__init__(driver)
        locators = JsonHandler.read_locators(os.path.join(path_config.conf_dir, "locators.json"))
        self.login_find_ele = locators['login_page']

    def click_jump_button(self):
        self.click(self.login_find_ele['jump_button'])

    def send_user_inpt(self,input_text):
        self.send_keys(self.login_find_ele['username_input'], input_text)

    def send_pass_input(self,pass_input):
        self.send_keys(self.login_find_ele['password_input'],pass_input)

    def click_login(self):
        self.click(self.login_find_ele['login_button'])

    def get_msg(self):
        time.sleep(1)
        try:
            msg = self.find_element_wait(self.login_find_ele['error_message']).text
        except(NoSuchElementException ,TimeoutException) as e:
            log.info(f"******************{self.driver.current_url}***************")
            msg = self.find_element_wait(self.login_find_ele['success']).text

        return msg

class LoginTask:

    def __init__(self,driver: webdriver.Remote):
        self.login_page = LoginPage(driver)



    def login(self,username,password):
        self.login_page.click_jump_button()
        self.login_page.send_user_inpt(username)
        self.login_page.send_pass_input(password)
        self.login_page.click_login()

        return self.login_page.get_msg()

